import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_1911/model/common_model.dart';
import 'package:flutter_1911/model/sales_model.dart';
import 'package:flutter_1911/until/nav_until.dart';
import 'package:flutter_1911/widget/webview.dart';

//底部卡片入口
class Sales extends StatelessWidget {
  final SalesModel sales;

  const Sales({Key? key, required this.sales}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: _item(context),
    );
  }

  _item(BuildContext context) {
    List<Widget> salesList = [];
    salesList
        //创建上面的两个图片
        .add(_double(context, sales.bigCard1, sales.bigCard2, true, false));
    salesList.add(
        _double(context, sales.smallCard1, sales.smallCard2, false, false));
    salesList
        .add(_double(context, sales.smallCard3, sales.smallCard4, false, true));
    return Column(
      children: [
        Container(
          height:312,
          margin: const EdgeInsets.only(left: 10),
          decoration: const BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: 1, color: Color(0xfff2f2f2)))),
          child: Column(
            //排列方式
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Image.network(
                    sales.icon,
                    width: 60,
                    height: 15,
                    //显示方式 填满
                    fit: BoxFit.fill,
                  ),
                  Container(
                    padding: const EdgeInsets.fromLTRB(10, 1, 8, 1),
                    margin: const EdgeInsets.only(left: 150),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12),
                      gradient: const LinearGradient(colors: [
                        //开始颜色
                        Colors.pink,
                        //终止颜色
                        Colors.pinkAccent,
                      ],  //线性渐变从左到右
                          begin: Alignment.centerLeft, end: Alignment.centerRight),
                    ),
                    //设置点击效果
                    child: GestureDetector(
                      onTap: () {
                        NavigatorUtil.push(
                          context,
                          HiWebView(url: sales.moreUrl, title: '更多活动'),
                        );
                      },
                      child: const Text(
                        '获取更多福利 >',
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                    ),
                  ),
                ],
              ),

              //把卡片设置进去
              Row(
                //控制控件的排序方式
                mainAxisAlignment: MainAxisAlignment.center,
                children: salesList.sublist(0, 1),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: salesList.sublist(1, 2),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: salesList.sublist(2, 3),
              )
            ],
          ),
        ),
      ],
    );
  }

  //两个参数分别代表左边的卡片模型和右边的卡片模型 还有大卡片和最后一个卡片
  Widget _double(BuildContext context, CommonModel leftCard,
      CommonModel rightCard, bool big, bool last) {
    //因为是行排列的 所以用row
    return Row(
      // mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        _saleItem(context, leftCard, big, true, last),
        _saleItem(context, rightCard, big, false, last)
      ],
    );
  }

  Widget _saleItem(
      BuildContext context, CommonModel model, bool big, bool left, bool last) {
    BorderSide borderSide = const BorderSide(width: 0.8, color: Color(0xfff2f2f2));
    return GestureDetector(

        onTap: () {
          NavigatorUtil.push(
              context,
              HiWebView(
                url: model.url,
                statusBarColor: model.statusBarColor,
                hideAppBar: model.hideAppBar,
              ));
        },
        child: Container(
          decoration: BoxDecoration(
            //设置分割线
              border: Border(
                //如果是left的话就设置一个border 不是的话就不设置
                  right: left
                      ? borderSide
                      : BorderSide.none,
                  //判断是否是最后一个 是最后一个的话就不设置分割线 不是最后一个的话就是设置分割线
                  bottom: last
                      ? BorderSide.none
                      : borderSide)),
          child: Image.network(
            model.icon!,
            //铺满
            fit: BoxFit.fill,
            width: MediaQuery.of(context).size.width / 2 - 10,
            //判断是否是大图 是大图的话设置129的高度 不是大图的话设置80的高度
            height: big ? 129 : 80,
          ),
        ));
  }
}
